<template>
  <div>
    <div class="search">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="类型">
          <el-input v-model="formData.name" placeholder="请输入类型" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getdata">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="dialogVisible = true">添加</el-button>
        </el-form-item>
      </el-form>

    </div>
    <div>
      <el-table :data="tableData" border>
        <el-table-column prop="id" label="序号" />
        <el-table-column prop="info" label="类别" />
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button size="small" type="danger" @click="remove(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        :page-size="limit"
        :total="total"
        style="padding:30px 0;text-align:center;"
        layout="total,prev,pager,next,jumper"
        @current-change="getPagedata"
      />
    </div>

    <el-dialog
      title="添加商品种类"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >

      <el-form :model="className" label-suffix=":">
        <el-form-item label="种类名称">
          <el-input v-model="className.name" autocomplete="off" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addclass">添加</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import productApi from '@/api/product/productset'
export default {
  data() {
    return {
      formData: {

      },
      tableData: null,
      current: 1, // 当前页
      limit: 10, // 每页显示记录数
      total: 0, // 总记录数
      dialogVisible: false,
      className: {}
    }
  },
  created() {
    this.getdata()
  },
  methods: {
    remove(id) {
      this.$confirm('此操作将永久删除,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // 确定执行then方法
        // 调用接口
        productApi.deleteHospSet(id)
          .then(response => {
            // 提示
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            // 刷新页面
            this.getdata()
          })
      })
    },
    // 获取数据
    getdata() {
      this.formData.page = this.current
      this.formData.limit = this.limit
      productApi.getclassification(this.formData).then(
        res => {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      )
    },
    // 分页获取
    getPagedata(page = 1) {
      this.current = page
      this.formData.page = this.current
      this.formData.limit = this.limit
      productApi.getclassification(this.formData).then(
        res => {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      )
    },
    addclass() {
      productApi.addclassification(this.className.name).then(
        res => {
          if (res.code === 500) {
            this.$message({
              type: 'warning',
              message: '该类型已存在!'
            })
          }
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '添加成功'
            })
            this.dialogVisible = false
            this.className = {}
            // 刷新页面
            this.getdata()
          }
        }
        // eslint-disable-next-line handle-callback-err
      )
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.$message('取消操作')
          done()
        })
        .catch(_ => {})
    }
  }
}
</script>

<style scoped lang="scss">

</style>
